<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Photo</title>
<link href="<?php echo(Yii::app()->params['app']['host'])?>/statics/web/css/style.css?v=<?php echo(Yii::app()->params['app']['version'])?>" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="<?php echo(Yii::app()->params['app']['host'])?>/statics/web/js/infownd.js?v=<?php echo(Yii::app()->params['app']['version'])?>" charset="utf-8"></script>
</head><body>
<script type="text/javascript">
var map;
var timer = null;
var clusters = [];
var markers = null;

function initialize() {
    var myOptions = {
        zoom : 4,
        center : new google.maps.LatLng(34.2, 108.8),
        mapTypeId : google.maps.MapTypeId.ROADMAP,
        navigationControlOptions:{style:google.maps.NavigationControlStyle.SMALL}
    };
    map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
    var myLatlng = new google.maps.LatLng(<?php echo $gps; ?>);
    var markers = new google.maps.Marker({ 
        position: myLatlng,  
        map: map, 
        icon: "<?php echo(Yii::app()->params['app']['host']); ?>/statics/web/images/point.png",
    });  
    if(markers){
        map.setZoom(12); 
        }
    openInfoWnd(markers);
    google.maps.event.addListener(markers, 'click', function(){
        openInfoWnd(markers);
    });
    map.setCenter(myLatlng);
    
    google.maps.event.addListener(map, 'click', function(){
        markers.open = false;
        markers.infoWnd.setMap(null);
    });
    google.maps.event.addListener(map, 'zoom_changed', function(){
        if (map.getZoom() < 3) {
            map.setZoom(3);
        }else if(map.getZoom() > 18){
            map.setZoom(18);
        }
        markers.open = false;
        markers.infoWnd.setMap(null);
    });
}

$(function(){
    initialize();
});

function drawCluster(){
    markers = [];
    var zoom = map.getZoom();
    for(var i=0, cluster; cluster = clusters[i]; i++){
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(cluster.center.lat, cluster.center.lng),
            icon: "<?php echo(Yii::app()->params['app']['host']); ?>/statics/web/images/point.png",
            map: map
        });
        
        marker.cluster = cluster;
        marker.open = false;
        marker.index = i;
        
        google.maps.event.addListener(marker, 'click', function(){
            openInfoWnd(this);
        });
        
        markers.push(marker);
    }
}
InfoBox.prototype.createElement = function() {
    var panes = this.getPanes();
    var div = this.div_;
    if (!div) {
        div = this.div_ = document.createElement("div");
        div.style.border = "0px none";
        div.style.position = "absolute";
        div.style.background = "url('<?php echo(Yii::app()->params['app']['host']); ?>/statics/web/images/infownd.png')";
        div.style.width = this.width_ + "px";
        div.style.height = this.height_ + "px";
        var contentDiv = document.createElement("div");
        contentDiv.innerHTML = this.content_;
        function removeInfoBox(ib) {
            return function() {
                ib.setMap(null);
            };
        }
        div.appendChild(contentDiv);
        div.style.display = 'none';
        panes.floatPane.appendChild(div);
    } else if (div.parentNode != panes.floatPane) {
        div.parentNode.removeChild(div);
        panes.floatPane.appendChild(div);
    } else {
    }
}

function openInfoWnd(marker){
    if(marker.open){
        return;
    }
    
    closeInfoWnd();
    
    marker.open = true;
    
    if(marker.infoWnd){
        marker.infoWnd.setMap(map);
        return;
    }
    var i = marker.index;
    var content = '<div class="infobox_content">'
         +'<img class="thumbnail" src="<?php echo(Yii::app()->params['app']['host']); ?>/index.php?r=thumbnail/index&size=normal&tid='+<?php echo $tid; ?>+'&ver='+<?php echo $vid; ?>+'"/>'
         +'<span class="photonum"> <?php if(strlen($name)>8){ echo CCharacter::utf8Substr($name, 0, 8) . '...'; }else{ echo $name; } ?></span>'
         +'<a class="viewlink" href="javascript:viewPhotos('+i+')"></a>'
          +'</div>';
    var infoBox = new InfoBox({latlng: marker.getPosition(), map: map, content: content});
    marker.infoWnd = infoBox;
}

function closeInfoWnd(){
    if(markers){
        for(var i=0; i<markers.length; i++){
            if(markers[i].open && markers[i].infoWnd){
                markers[i].open = false;
                markers[i].infoWnd.setMap(null);
            }
        }
    }
}

function rad(d){
    return d * Math.PI / 180.0;
}


function addPhotoToCluster(cluster, photo){
    cluster.photos.push(photo);
    var len = cluster.photos.length;
    if(len==1) {
        cluster.center.lat = photo.lat;
        cluster.center.lng = photo.lng;
        return;
    }
    var lat, lng;
    
    if(Math.abs(cluster.center.lng - photo.lng)>180){
        cluster.center.lng < 0 && (cluster.center.lng += 360);
        photo.lng < 0 && (photo.lng += 360);
    }
    lng = (cluster.center.lng*(len-1)+parseFloat(photo.lng))/len;
    lat = (cluster.center.lat*(len-1)+parseFloat(photo.lat))/len;
    
    lng > 180 && (lng -= 360);
    cluster.center.lat = lat;
    cluster.center.lng = lng;
}

function isPhotoInCluster(cluster, photo){
    var center = cluster.center;
    var d = getDistance({lat:center.lat, lng:center.lng}, {lat:photo.lat, lng:photo.lng});
    
    var zoom = map.getZoom();
    
    var dst;
    switch(zoom){
    case 3:
        dst = 200000;
        break;
    case 4:
        dst = 100000;
        break;
    case 5:
        dst = 80000;
        break;
    case 6:
        dst = 60000;
        break;
    case 7:
        dst = 40000;
        break;
    case 8:
        dst = 20000;
        break;
    case 9:
        dst = 10000;
        break;
    case 10:
        dst = 5000;
        break;
    case 11:
        dst = 2000;
        break;
    case 12:
        dst = 1000;
        break;
    case 13:
        dst = 800;
        break;
    case 14:
        dst = 600;
        break;
    case 15:
        dst = 400;
        break;
    case 16:
        dst = 200;
        break;
    case 17:
        dst = 50;
        break;
    case 18:
        dst = 10;
    }
    
    return d <= dst;
}

function viewPhotos(i){
    parent.parent.PageCTL.GOTO_URL("<?php echo(Yii::app()->params['app']['host']); ?>/index.php?r=List/index&aid=3&cid=<?php echo $cid; ?>&tpl=<?php echo $tpl; ?>");
}
</script>
<body style="height: 100%">
<div id="map_canvas"  style="width: 100%; height: 100%; margin: 0px auto; border: 1px solid #ccc;">
</div>
</body></html>